local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

function scene:updateLevelCallback()
return function(delta)
   self.gameWorld:update(delta)
end
end

function scene:testCircles()
end

function scene:testAtlasses()
   local spriteSheetInfo = require('resources.sprites.atlasses.sheet')
   local spriteSheet     = graphics.newImageSheet("resources/sprites/atlasses/sheet.png", spriteSheetInfo:getSheet())
   local sequenceData = {
      { name="seq1", sheet=spriteSheet, start=1, count=3, time=500, loopCount=0 },
      { name="seq2", sheet=spriteSheet, start=5, count=2, time=500, loopCount=0 }
   }

   local myAnimation = display.newSprite(self.view,spriteSheet, sequenceData )
   myAnimation.x,myAnimation.y = X*0.9,Y*0.8
   myAnimation:scale(0.5,-0.5)
   myAnimation:play()

   local timer1 = timer.performWithDelay(1000,function()
      myAnimation.timeScale = 0.25
      logger:debug('cuack1')
   end)
   local timer2 = timer.performWithDelay(3000,function()
      myAnimation:setSequence('seq2')
      myAnimation:play()
      logger:debug('cuack2')
   end)

   table.insert(self.timers,timer1)
   table.insert(self.timers,timer2)
end

function scene:testHardware()
   local eventText = display.newText({text='Waiting event ...' .. lfs.currentdir(), x=halfX, y=halfY, fontsize=3, parent=self.view})
   eventText:setFillColor(1,1,0)

   local listener = function(event)
      eventText.text = inspect(event)
      logger:debug(inspect(event))
   end

   timer.performWithDelay(2000,
      function()
         Runtime:addEventListener("accelerometer",listener)
      end
   )
end

function scene:testButton()
   local spriteSheetInfo = require('resources.sprites.atlasses.sheet')
   local spriteSheet     = graphics.newImageSheet("resources/sprites/atlasses/sheet.png", spriteSheetInfo:getSheet())
   local sequenceData = {
      { name="normal" , sheet=spriteSheet, start=1, count=1,loopCount=1},
      { name="pressed", sheet=spriteSheet, start=3, count=1,loopCount=1}
   }

   local button = display.newSprite(self.view,spriteSheet, sequenceData )
   button.x,button.y = X*0.5,Y*0.5
   button:scale(0.5,0.5)

   DisplayObjectUtils.addTapEventListener(
      button,
      function()
         logger:debug('ABC')
         storyboard.gotoScene("scripts.scenes.mainScene",{effect = "fade",time = 100})
      end,
      'normal','pressed'
   )
end

function scene:createGameObjects()
   self.gameWorld = GameWorld:new()
   self.view:insert(self.gameWorld)

   self:testCircles()
   self:testAtlasses()
   self:testHardware()
   self:testHardware()
   self:testButton()
end

--Methods
function scene:createScene(event)
   logger:gameEvent("Create Test Scene")
   storyboard.removeAll()
   self.timers = {}

   scene:createGameObjects()
end
function scene:enterScene(event)
   logger:gameEvent("Enter Test Scene")

   GameController.getInstance().addCallback(scene:updateLevelCallback(), 'mainLoop')
   GameController.getInstance():beginGame()
end

function scene:exitScene(event)
   logger:gameEvent("exit Test Scene")

   GameController.getInstance().clearAllCallbacks()
   GameController.getInstance():stop()
end

function scene:destroyScene(event)
   logger:gameEvent("destroy Test Scene")

   for i=#self.timers,1,-1 do
      timer.cancel(self.timers[i])
      table.remove(self.timers,i)
   end

   GameController.getInstance().clearAllCallbacks()
   GameController.getInstance():stop()
end

scene:addEventListener("createScene",scene)
scene:addEventListener("enterScene",scene)
scene:addEventListener("exitScene",scene)
scene:addEventListener("destroyScene", scene)

return scene



